package a66weding.com.jiehuntong.fragment.category.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.andview.refreshview.recyclerview.BaseRecyclerAdapter;
import com.baidu.platform.comapi.map.C;

import org.w3c.dom.Text;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.CategorySearchActivity;
import a66weding.com.jiehuntong.activity.LoginActivity;
import a66weding.com.jiehuntong.activity.home.hotel.shop.HomeProductdetailActivity;
import a66weding.com.jiehuntong.application.GlobalApplication;
import a66weding.com.jiehuntong.beans.CancelCollectBean;
import a66weding.com.jiehuntong.beans.ErrorBean;
import a66weding.com.jiehuntong.beans.HomeBotomListBean;
import a66weding.com.jiehuntong.beans.HomeHeaderLooperBean;
import a66weding.com.jiehuntong.beans.HttpRequestBean;
import a66weding.com.jiehuntong.beans.MeShoppingCartDeleteBean;
import a66weding.com.jiehuntong.beans.PackageBean;
import a66weding.com.jiehuntong.beans.ShoplistBean;
import a66weding.com.jiehuntong.beans.SuccessBean;
import a66weding.com.jiehuntong.common.utils.ImageOptionsUtil;
import a66weding.com.jiehuntong.fragment.category.PackageFragment;
import a66weding.com.jiehuntong.http.HttpHandleUtil;
import a66weding.com.jiehuntong.http.OnNetResultListener;
import a66weding.com.jiehuntong.http.RequestCode;
import a66weding.com.jiehuntong.http.UrlDealUtil;
import a66weding.com.jiehuntong.inner.OnItemClickLitener;
import a66weding.com.jiehuntong.view.ConfirmLoginDialog;
import a66weding.com.jiehuntong.widgets.CircularImage;
import a66weding.com.jiehuntong.widgets.dialog.MyRedheartDialog;

/**
 * Created by admin on 2016/8/6.
 * 作用：套餐适配器
 */
public class PackAgeAdapter  extends BaseRecyclerAdapter<PackAgeAdapter.ViewHolder> implements OnNetResultListener {
    private boolean isLogin;
    private boolean isCollection;
    //如果有点击事件请设置一个监听;
    private OnItemClickLitener mOnItemClickLitener;
    private  final int  DIALOG=1;
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DIALOG:
                    if(myDialog!=null) {
                        myDialog.dismiss();
                    }
                    break;

            }
        }
    };
    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
    private CategorySearchActivity context;
    private PackageFragment packageFragment;
    //map集合;
    private static HashMap<Integer, Boolean> map = new HashMap<>();
    private List<HomeBotomListBean.DataBean> data = new ArrayList<>();

    public PackAgeAdapter(PackageFragment context,List<HomeBotomListBean.DataBean> data){
        this.context = (CategorySearchActivity) context.getActivity();
        this.packageFragment =  context;
        if (data!=null){
            this.data = data;
        }
    }

    @Override
    public ViewHolder getViewHolder(View view) {
        return new ViewHolder(view);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType, boolean isItem) {
        View v =  LayoutInflater.from(context).inflate(R.layout.item_package,parent, false);
        ViewHolder viewHolder = new ViewHolder(v);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position, boolean isItem) {
        if (mOnItemClickLitener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    mOnItemClickLitener.onItemClick(holder.itemView,pos);
                }
            });
        }
        isLogin = ((GlobalApplication)context.getApplicationContext()) .isLogin();
        if (data.get(position)!=null){
            isCollection = data.get(position).isIsCollection();
            x.image().bind(holder.iv_homebusitem_business,data.get(position).getNvc_PicMain(), ImageOptionsUtil.getOptions316_720());
            holder.item_pack_title.setText(data.get(position).getNvc_Name());
            holder.tv_homelist_itemprice.setText(""+data.get(position).getDm_ShopPrice());
            holder.tv_homelist_itemmarketprice.setText(""+data.get(position).getDm_Price());
            //设置中划线 并高清;
            holder.tv_homelist_itemmarketprice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
            x.image().bind(holder.ic_launcher,data.get(position).getBusiness().getNvc_Logo(), ImageOptionsUtil.getcircle_fixy());
            //如果收藏了
            if (isCollection){
                holder.iv_home_collect.setImageResource(R.mipmap.storyshoucang_after);
            }else{
                holder.iv_home_collect.setImageResource(R.mipmap.storyshoucang);
            }
            holder.pack_shoucang.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //如果是收藏;
                    if (isLogin){
                        doCollection(position);
                        notifyDataSetChanged();
                    }else{
                        packageFragment.savePosition(position);
                        showLoginDialog();
                    }
                }
            });
            //点击 进入商品详情
            holder.iv_homelist_toshoping.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int i_id = data.get(position).getI_Id();
                    Intent intent = new Intent(context, HomeProductdetailActivity.class);
                    intent.putExtra("productId",i_id);
                   context.startActivity(intent);
                }
            });
        }
    }
    public  void showLoginDialog() {
        final ConfirmLoginDialog confirmDialog = new ConfirmLoginDialog(context, "亲，需要登录哦~", "去登录", "取消",R.layout.pop_tologin);
        confirmDialog.show();
        confirmDialog.setClicklistener(new ConfirmLoginDialog.ClickListenerInterface() {
            @Override
            public void doConfirm() {
                Intent intent = new Intent(context, LoginActivity.class);
                packageFragment.startActivityForResult(intent, packageFragment.REQUEST_LOGIN_CODE);
                confirmDialog.dismiss();
            }
            @Override
            public void doCancel() {
                confirmDialog.dismiss();
            }
        });
    }
    public void doCollection(int position){
        if (isCollection){
            //取消收藏;
            String oldUrl= UrlDealUtil.getRealUrlNewIdApi(RequestCode.POSTCOLLECTIONCANCEL);
            String url = oldUrl + "&productID=" + data.get(position).getI_Id();
            HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                    PackAgeAdapter.this, CancelCollectBean.class, RequestCode.POSTCOLLECTIONCANCEL);
            HttpHandleUtil.executeCommonPost(context,httpRequestBean);
            data.get(position).setIsCollection(false);
        }else {
            //添加收藏;
            String oldUrl= UrlDealUtil.getRealUrlNewIdApi(RequestCode.POSTCOLLECTIONCREATE);
            String url = oldUrl + "&productID=" + data.get(position).getI_Id() + "&businessID=" + data.get(position).getI_Business();
            HttpRequestBean httpRequestBean = new HttpRequestBean(RequestCode.POSTEQUEST, url,
                    PackAgeAdapter.this, MeShoppingCartDeleteBean.class, RequestCode.POSTCOLLECTIONCREATE);
            HttpHandleUtil.executeCommonPost(context,httpRequestBean);
            data.get(position).setIsCollection(true);
        }
    }

    @Override
    public int getAdapterItemCount() {
        return this.data.size();
    }

    private MyRedheartDialog myDialog;

    @Override
    public void onSucess(SuccessBean sucessbean) {
        Object data = sucessbean.getData();
        if(data instanceof MeShoppingCartDeleteBean){      // 加入收藏成功后
            myDialog = new MyRedheartDialog(context,"收藏成功!");
            myDialog.show();
        }else if(data instanceof CancelCollectBean){      // 删除收藏成功后
            myDialog = new MyRedheartDialog(context,"取消收藏成功!");
            myDialog.show();
        }
        Message message = mHandler.obtainMessage();
        message.what  = DIALOG;
        mHandler.sendMessageDelayed(message,800);
    }

    @Override
    public void onError(ErrorBean errorBean) {

    }

    public  class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView iv_homebusitem_business;
        private TextView item_pack_title;
        private TextView tv_homelist_itemprice;
        private TextView tv_homelist_itemmarketprice;
        private CircularImage ic_launcher;
        private RelativeLayout pack_shoucang;
        private ImageView iv_home_collect;
        private LinearLayout iv_homelist_toshoping;
        public ViewHolder(View itemView) {
            super(itemView);
            iv_homebusitem_business = (ImageView) itemView.findViewById(R.id.iv_homebusitem_business);
            item_pack_title = (TextView) itemView.findViewById(R.id.item_pack_title);
            tv_homelist_itemprice = (TextView) itemView.findViewById(R.id.tv_homelist_itemprice);
            tv_homelist_itemmarketprice = (TextView) itemView.findViewById(R.id.tv_homelist_itemmarketprice);
            ic_launcher = (CircularImage) itemView.findViewById(R.id.package_icon_item);
            pack_shoucang = (RelativeLayout) itemView.findViewById(R.id.pack_shoucang);
            iv_home_collect = (ImageView) itemView.findViewById(R.id.iv_home_collect);
            iv_homelist_toshoping = (LinearLayout) itemView.findViewById(R.id.iv_homelist_toshoping);
        }
    }


    public void append(List<HomeBotomListBean.DataBean> data) {
        if (this.data != null) {
            this.data.addAll(data);
        } else {
            this.data = data;
        }
        notifyDataSetChanged();
    }
    public void clear() {
        if (this.data != null) {
            this.data.clear();
        }
    }
    public int getSize(){
        if (this.data==null){
            return 0;
        }
        return this.data.size();
    }

    public static HashMap<Integer, Boolean> getMap(){
        return map;
    }

}
